<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车 - 泡泡玛特</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #8a2be2;
            --secondary: #ff6ec7;
            --accent: #00c8ff;
            --light: #f9f5ff;
            --dark: #2a0a4a;
        }

        body {
            background-color: #f8f7fc;
            color: #333;
            overflow-x: hidden;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0 20px;
        }

        /* 头部导航 */
        header {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            box-shadow: 0 4px 12px rgba(138, 43, 226, 0.2);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .header-top {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 0;
        }

        .logo {
            display: flex;
            align-items: center;
            color: white;
            text-decoration: none;
            font-size: 28px;
            font-weight: 800;
        }

        .logo i {
            margin-right: 10px;
            font-size: 32px;
        }

        .search-bar {
            flex: 1;
            max-width: 600px;
            margin: 0 30px;
            position: relative;
        }

        .search-bar input {
            width: 100%;
            padding: 12px 20px;
            border-radius: 30px;
            border: none;
            font-size: 16px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }

        .search-bar button {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            background: var(--accent);
            border: none;
            color: white;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            cursor: pointer;
        }

        .nav-icons {
            display: flex;
            gap: 20px;
        }

        .nav-icon {
            color: white;
            font-size: 22px;
            position: relative;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .nav-icon-text {
            font-size: 16px;
            font-weight: 500;
        }

        .cart-count {
            position: absolute;
            top: -8px;
            right: -8px;
            background: var(--accent);
            color: white;
            font-size: 12px;
            width: 20px;
            height: 20px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }

        /* 主菜单 */
        .main-nav {
            background: rgba(255, 255, 255, 0.15);
            backdrop-filter: blur(10px);
        }

        .main-nav ul {
            display: flex;
            list-style: none;
            padding: 12px 0;
        }

        .main-nav li {
            margin-right: 30px;
        }

        .main-nav a {
            color: white;
            text-decoration: none;
            font-weight: 600;
            font-size: 16px;
            padding: 8px 0;
            position: relative;
        }

        .main-nav a:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 0;
            height: 2px;
            background: var(--accent);
            transition: width 0.3s;
        }

        .main-nav a:hover:after {
            width: 100%;
        }

        /* 购物车内容 */
        .cart-container {
            margin: 40px 0;
            display: flex;
            gap: 30px;
        }

        .cart-items {
            flex: 2;
            background: white;
            border-radius: 16px;
            padding: 25px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        .cart-summary {
            flex: 1;
            background: white;
            border-radius: 16px;
            padding: 25px;
            height: fit-content;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        }

        .cart-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 20px;
            border-bottom: 1px solid #eee;
            margin-bottom: 20px;
        }

        .cart-header h2 {
            font-size: 24px;
            color: var(--dark);
        }

        .cart-empty {
            text-align: center;
            padding: 50px 0;
        }

        .cart-empty i {
            font-size: 80px;
            color: #ddd;
            margin-bottom: 20px;
        }

        .cart-empty h3 {
            font-size: 22px;
            color: #777;
            margin-bottom: 15px;
        }

        .cart-empty p {
            color: #999;
            margin-bottom: 25px;
        }

        .btn-shopping {
            display: inline-block;
            padding: 12px 32px;
            background: var(--primary);
            color: white;
            text-decoration: none;
            font-weight: 600;
            border-radius: 30px;
            transition: all 0.3s;
            box-shadow: 0 4px 15px rgba(138, 43, 226, 0.4);
        }

        .btn-shopping:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(138, 43, 226, 0.6);
        }

        .cart-item {
            display: flex;
            padding: 20px 0;
            border-bottom: 1px solid #f5f5f5;
        }

        .item-info {
            flex: 1;
            padding: 0 20px;
        }

        .item-info h3 {
            font-size: 18px;
            margin-bottom: 10px;
            color: var(--dark);
        }

        .item-price {
            font-size: 18px;
            font-weight: 700;
            color: var(--primary);
            margin-bottom: 15px;
        }

        .item-actions {
            display: flex;
            align-items: center;
        }

        .quantity-control {
            display: flex;
            align-items: center;
            margin-right: 20px;
        }

        .quantity-control button {
            width: 30px;
            height: 30px;
            border: none;
            background: #f0f0f0;
            color: #666;
            font-size: 16px;
            cursor: pointer;
            border-radius: 4px;
        }

        .quantity-control input {
            width: 50px;
            height: 30px;
            text-align: center;
            border: 1px solid #ddd;
            margin: 0 10px;
            border-radius: 4px;
        }

        .remove-item {
            color: #ff4d4f;
            background: none;
            border: none;
            cursor: pointer;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .remove-item:hover {
            color: #ff7875;
        }

        .summary-title {
            font-size: 20px;
            color: var(--dark);
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        .summary-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
        }

        .summary-label {
            color: #666;
        }

        .summary-value {
            font-weight: 600;
        }

        .summary-total {
            font-size: 18px;
            margin-top: 10px;
            padding-top: 15px;
            border-top: 1px solid #eee;
        }

        .summary-total .summary-value {
            color: var(--primary);
            font-size: 22px;
        }

        .btn-checkout {
            display: block;
            width: 100%;
            padding: 15px;
            background: var(--secondary);
            color: white;
            text-align: center;
            font-weight: 600;
            border-radius: 30px;
            margin-top: 20px;
            border: none;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s;
            box-shadow: 0 4px 15px rgba(255, 110, 199, 0.4);
        }

        .btn-checkout:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(255, 110, 199, 0.6);
        }

        /* 页脚 */
        footer {
            background: var(--dark);
            color: white;
            padding: 60px 0 30px;
            margin-top: 60px;
        }

        .footer-content {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 30px;
        }

        .footer-column h3 {
            font-size: 20px;
            margin-bottom: 20px;
            position: relative;
            padding-bottom: 10px;
        }

        .footer-column h3:after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 40px;
            height: 3px;
            background: var(--accent);
        }

        .footer-column ul {
            list-style: none;
        }

        .footer-column ul li {
            margin-bottom: 12px;
        }

        .footer-column a {
            color: #ccc;
            text-decoration: none;
            transition: all 0.3s;
        }

        .footer-column a:hover {
            color: var(--accent);
            padding-left: 5px;
        }

        .social-links {
            display: flex;
            gap: 15px;
            margin-top: 20px;
        }

        .social-links a {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.1);
            color: white;
            font-size: 18px;
            transition: all 0.3s;
        }

        .social-links a:hover {
            background: var(--accent);
            transform: translateY(-5px);
        }

        .copyright {
            text-align: center;
            padding-top: 30px;
            margin-top: 30px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            color: #aaa;
            font-size: 14px;
        }

        /* 响应式设计 */
        @media (max-width: 900px) {
            .cart-container {
                flex-direction: column;
            }

            .footer-content {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        @media (max-width: 768px) {
            .header-top {
                flex-direction: column;
                gap: 15px;
            }

            .search-bar {
                margin: 10px 0;
                max-width: 100%;
            }

            .main-nav ul {
                overflow-x: auto;
                padding-bottom: 5px;
            }

            .nav-icon-text {
                display: none;
            }

            .cart-item {
                flex-direction: column;
                align-items: center;
                text-align: center;
            }

            .item-info {
                padding: 15px 0;
            }

            .item-actions {
                justify-content: center;
            }
        }

        @media (max-width: 480px) {
            .footer-content {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<!-- 顶部导航 -->
<header>
    <div class="container">
        <div class="header-top">
            <a href="index.html" class="logo">
                <i class="fas fa-gem"></i>
                POP MART
            </a>

            <div class="search-bar">
                <input type="text" placeholder="搜索潮玩、艺术家系列...">
                <button><i class="fas fa-search"></i></button>
            </div>

            <div class="nav-icons">
                <!-- 登录状态显示 -->
                <div class="nav-icon" id="userIcon">
                    <i class="fas fa-user"></i>
                    <span class="nav-icon-text" id="loginText">登录</span>
                </div>
                <div class="nav-icon">
                    <i class="fas fa-heart"></i>
                    <span class="nav-icon-text">收藏</span>
                </div>
                <div class="nav-icon" id="cartIcon">
                    <i class="fas fa-shopping-cart"></i>
                    <span class="cart-count" id="cartCount">0</span>
                    <span class="nav-icon-text">购物车</span>
                </div>
            </div>
        </div>

        <nav class="main-nav">
            <ul>
                <li><a href="index.html">首页</a></li>
                <li><a href="#">热门分类</a></li>
                <li><a href="#">新品上市</a></li>
                <li><a href="#">热门系列</a></li>
                <li><a href="#">盲盒专区</a></li>
                <li><a href="#">艺术家</a></li>
                <li><a href="#">限定商品</a></li>
                <li><a href="#">促销活动</a></li>
                <li><a href="order.html">我的订单</a></li>
                <li><a href="#">会员中心</a></li>
            </ul>
        </nav>
    </div>
</header>

<!-- 购物车内容 -->
<div class="container">
    <div class="cart-container">
        <div class="cart-items">
            <div class="cart-header">
                <h2>我的购物车</h2>
                <span id="cartItemCount">0件商品</span>
            </div>

            <div class="cart-content" id="cartContent">
                <!-- 购物车为空状态 -->
                <div class="cart-empty">
                    <i class="fas fa-shopping-cart"></i>
                    <h3>购物车还是空的</h3>
                    <p>快去选购心仪的商品吧</p>
                    <a href="index.html" class="btn-shopping">去逛逛</a>
                </div>

                <!-- 购物车商品列表 -->
                <!--
                <div class="cart-item">
                    <div class="item-image">
                        <img src="https://via.placeholder.com/120" alt="商品图片">
                    </div>
                    <div class="item-info">
                        <h3>Molly 奇幻梦境系列 - 隐藏款</h3>
                        <p class="item-price">¥99.00</p>
                        <div class="item-actions">
                            <div class="quantity-control">
                                <button class="decrease">-</button>
                                <input type="number" class="quantity" value="1" min="1">
                                <button class="increase">+</button>
                            </div>
                            <button class="remove-item">
                                <i class="fas fa-trash"></i> 删除
                            </button>
                        </div>
                    </div>
                </div>
                -->
            </div>
        </div>

        <div class="cart-summary">
            <h3 class="summary-title">订单摘要</h3>

            <div class="summary-row">
                <span class="summary-label">商品总价</span>
                <span class="summary-value" id="subtotal">¥0.00</span>
            </div>

            <div class="summary-row">
                <span class="summary-label">运费</span>
                <span class="summary-value">¥0.00</span>
            </div>

            <div class="summary-row">
                <span class="summary-label">优惠</span>
                <span class="summary-value">-¥0.00</span>
            </div>

            <div class="summary-row summary-total">
                <span class="summary-label">总计</span>
                <span class="summary-value" id="total">¥0.00</span>
            </div>

            <button class="btn-checkout" id="checkoutBtn">结算</button>
        </div>
    </div>
</div>

<!-- 页脚 -->
<footer>
    <div class="container">
        <div class="footer-content">
            <div class="footer-column">
                <h3>关于我们</h3>
                <ul>
                    <li><a href="#">品牌故事</a></li>
                    <li><a href="#">企业文化</a></li>
                    <li><a href="#">联系我们</a></li>
                    <li><a href="#">加入我们</a></li>
                    <li><a href="#">门店查询</a></li>
                </ul>
            </div>

            <div class="footer-column">
                <h3>客户服务</h3>
                <ul>
                    <li><a href="#">常见问题</a></li>
                    <li><a href="#">配送说明</a></li>
                    <li><a href="#">退换政策</a></li>
                    <li><a href="#">会员制度</a></li>
                    <li><a href="#">防伪查询</a></li>
                </ul>
            </div>

            <div class="footer-column">
                <h3>商务合作</h3>
                <ul>
                    <li><a href="#">IP合作</a></li>
                    <li><a href="#">渠道合作</a></li>
                    <li><a href="#">媒体合作</a></li>
                    <li><a href="#">企业采购</a></li>
                    <li><a href="#">展会活动</a></li>
                </ul>
            </div>

            <div class="footer-column">
                <h3>关注我们</h3>
                <p>获取最新潮玩资讯和独家优惠</p>
                <div class="social-links">
                    <a href="#"><i class="fab fa-weibo"></i></a>
                    <a href="#"><i class="fab fa-weixin"></i></a>
                    <a href="#"><i class="fab fa-qq"></i></a>
                    <a href="#"><i class="fab fa-douban"></i></a>
                    <a href="#"><i class="fab fa-tiktok"></i></a>
                </div>
            </div>
        </div>

        <div class="copyright">
            <p>© 2023 POP MART 泡泡玛特 版权所有 | 潮流玩具购物平台</p>
            <p>京ICP备12345678号 | 客服电话：400-123-4567</p>
        </div>
    </div>
</footer>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 购物车图标点击事件
        const cartIcon = document.getElementById('cartIcon');
        cartIcon.addEventListener('click', function() {
            const savedUser = localStorage.getItem('popmartUser');
            if (savedUser) {
                const user = JSON.parse(savedUser);
                window.location.href = `cart.html?userId=${user.uId}`;
            } else {
                window.location.href = 'login.html';
            }
        });

        // 从URL获取用户ID
        const urlParams = new URLSearchParams(window.location.search);
        let userId = urlParams.get('userId');

        // 如果没有URL参数，尝试从本地存储获取用户ID
        if (!userId) {
            const savedUser = localStorage.getItem('popmartUser');
            if (savedUser) {
                const user = JSON.parse(savedUser);
                userId = user.uId;
            } else {
                window.location.href = 'login.html';
                return;
            }
        }

        // 更新购物车商品数量
        function updateCartCount(count) {
            document.getElementById('cartCount').textContent = count;
        }

        // 获取购物车数据
        async function fetchCartData(userId) {
            try {
                const response = await fetch(`/shop/cart/${userId}`);
                console.log("购物车中的数据！");
                console.log(response);
                if (!response.ok) {
                    throw new Error('获取购物车数据失败');
                }
                return await response.json();
            } catch (error) {
                console.error('获取购物车数据失败:', error);
                return [];
            }
        }

        // 渲染购物车商品
        function renderCartItems(cartItems) {
            const cartContent = document.getElementById('cartContent');
            const itemCount = cartItems.length;
            const cartItemCount = document.getElementById('cartItemCount');

            cartItemCount.textContent = `${itemCount}件商品`;
            updateCartCount(itemCount);

            if (itemCount === 0) {
                // 显示购物车为空状态
                cartContent.innerHTML = `
                    <div class="cart-empty">
                        <i class="fas fa-shopping-cart"></i>
                        <h3>购物车还是空的</h3>
                        <p>快去选购心仪的商品吧</p>
                        <a href="index.html" class="btn-shopping">去逛逛</a>
                    </div>
                `;
                updateCartSummary(0);
                return;
            }

            // 渲染商品列表
            let cartHTML = '';
            let subtotal = 0;

            cartItems.forEach(item => {
                const itemTotal = item.gPrice * item.sNum;
                subtotal += itemTotal;

                cartHTML += `
                    <div class="cart-item" data-id="${item.sId}">
                        <div class="item-info">
                            <h3>${item.gName}</h3>
                            <p class="item-price">¥${item.gPrice}</p>
                            <div class="item-actions">
                                <div class="quantity-control">
                                    <button class="decrease">-</button>
                                    <input type="number" class="quantity" value="${item.sNum}" min="1">
                                    <button class="increase">+</button>
                                </div>
                                <button class="remove-item">
                                    <i class="fas fa-trash"></i> 删除
                                </button>
                            </div>
                        </div>
                    </div>
                `;
            });

            cartContent.innerHTML = cartHTML;
            updateCartSummary(subtotal);

            // 添加事件监听
            document.querySelectorAll('.decrease').forEach(button => {
                button.addEventListener('click', function() {
                    const input = this.nextElementSibling;
                    if (parseInt(input.value) > 1) {
                        input.value = parseInt(input.value) - 1;
                        updateCartItem(this.closest('.cart-item'));
                    }
                });
            });

            document.querySelectorAll('.increase').forEach(button => {
                button.addEventListener('click', function() {
                    const input = this.previousElementSibling;
                    input.value = parseInt(input.value) + 1;
                    updateCartItem(this.closest('.cart-item'));
                });
            });

            document.querySelectorAll('.quantity').forEach(input => {
                input.addEventListener('change', function() {
                    if (parseInt(this.value) < 1) {
                        this.value = 1;
                    }
                    updateCartItem(this.closest('.cart-item'));
                });
            });

            document.querySelectorAll('.remove-item').forEach(button => {
                button.addEventListener('click', function() {
                    removeCartItem(this.closest('.cart-item'));
                });
            });
        }

        // 更新购物车摘要
        function updateCartSummary(subtotal) {
            document.getElementById('subtotal').textContent = `¥${subtotal.toFixed(2)}`;
            document.getElementById('total').textContent = `¥${subtotal.toFixed(2)}`;
        }

        // 更新购物车商品数量
        async function updateCartItem(cartItem) {
            const itemId = cartItem.dataset.id;
            const quantity = parseInt(cartItem.querySelector('.quantity').value);

            try {
                const response = await fetch('/shop/updateCart', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        sId: parseInt(itemId),
                        sNum: quantity
                    })
                });

                const result = await response.json();
                if (result.success) {
                    // 重新加载购物车数据
                    loadCartData();
                } else {
                    alert('更新购物车失败');
                }
            } catch (error) {
                console.error('更新购物车失败:', error);
                alert('更新购物车失败');
            }
        }

        // 删除购物车商品
        async function removeCartItem(cartItem) {
            const itemId = cartItem.dataset.id;

            try {
                const response = await fetch(`/shop/${itemId}`, {
                    method: 'DELETE'
                });

                if (response.ok) {
                    // 从DOM中移除
                    cartItem.remove();
                    // 重新加载购物车数据
                    loadCartData();
                } else {
                    alert('删除商品失败');
                }
            } catch (error) {
                console.error('删除商品失败:', error);
                alert('删除商品失败');
            }
        }

        // 加载购物车数据
        async function loadCartData() {
            if (!userId) {
                alert('用户未登录');
                window.location.href = 'login.html';
                return;
            }

            const cartItems = await fetchCartData(userId);
            renderCartItems(cartItems);
        }

        // 结算按钮事件
        document.getElementById('checkoutBtn').addEventListener('click', function() {
            if (!userId) {
                alert('请先登录');
                window.location.href = 'login.html';
                return;
            }

            // 跳转到订单页面
            window.location.href = `order.html?userId=${userId}`;
        });

        // 初始化页面
        loadCartData();

        // 用户登录状态检查
        const savedUser = localStorage.getItem('popmartUser');
        if (savedUser) {
            const user = JSON.parse(savedUser);
            document.getElementById('loginText').textContent = user.uName;
        }
    });
</script>
</body>
</html>